package com.shawn.baseappbygroovydsl.ui.views

import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import eee.drama.idea_chat.bean.BeanUserSpeak
import com.shawn.baseappbygroovydsl.spf.SPFAppConfig
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.shawn.baseappbygroovydsl.ui.adapter.ChatContentListAdapter
import eee.drama.idea_chat.bean.MessageChatBean
import kotlin.random.Random

/**
 * @author Secret
 * @since 2023/5/16
 */
class VMChat : ViewModel() {

    private val gson by lazy {
        Gson()
    }

    private var initUserSize = 20

    private val userSpeakLiveData = MutableLiveData<MutableList<BeanUserSpeak>>()
    private val userGroupLiveData = MutableLiveData<MutableList<MessageChatBean>>()
    private val userResultList = mutableListOf<MessageChatBean>()
    val userResultLiveData = MediatorLiveData<MutableList<MessageChatBean>>()
    val replyCommentsLiveData = MediatorLiveData<MutableList<MessageChatBean>>()

    init {
        userResultLiveData.addSource(userGroupLiveData) {
            computeResult(false)
        }
        userResultLiveData.addSource(userSpeakLiveData) {
            computeResult(false)
        }
    }

    private fun getUserSpeakData(size: Int): MutableList<MessageChatBean> {
        val refreshResultList = mutableListOf<MessageChatBean>()
        val userGroup = userGroupLiveData.value
        val userSpeak = userSpeakLiveData.value
        if (userGroup != null && userSpeak != null) {
            // 打乱集合的数据
            userGroup.shuffle()
            userSpeak.shuffle()
            // 清空刷新的数据集合
            refreshResultList.clear()
            // 获取说话数据的长度
            val speakSize = userSpeak.size
            // 获取说话用户的长度
            val userSize = userGroup.size

            for (i in 0 until size) {
                // 生成说话用户的随机数
                val userIndex = Random.nextInt(0, userSize)
                // 获取说话用户
                val user = userGroup[userIndex]
                // 生成说话内容的随机数
                val speakIndex = Random.nextInt(0, speakSize)
                // 获取说话内容
                val msg = userSpeak[speakIndex]
                // 将说的话 给 要说的用户
                user.msg = msg.speak

                if (i > 20) {
                    // 随意红包显示的索引
                    val speakIndexTemp = Random.nextInt(0, refreshResultList.size)
                    // 没有红包需要添加1个
                    refreshResultList[speakIndexTemp].itemType = getChatRedStyleType()
                } else {
                    if (size == 1) {
                        // 生成一个随机小数
                        val randomValue = Random.nextDouble()
                        // 随机生成红包
                        if (randomValue < 0.41) {
                            user.itemType = getChatRedStyleType()
                        } else {
                            user.itemType = msg.itemType
                        }
                    } else {
                        user.itemType = msg.itemType
                    }
                }

                // 将说话用户放进展示的集合中
                refreshResultList.add(user)
            }
        }
        return refreshResultList
    }

    private fun getChatRedStyleType(): Int {
//        return ChatContentListAdapter.ITEM_TYPE_RED_STYLE_2
        return when (Random.nextInt(0, 3)) {
            0 -> ChatContentListAdapter.ITEM_TYPE_RED_STYLE_0
            1 -> ChatContentListAdapter.ITEM_TYPE_RED_STYLE_1
            2 -> ChatContentListAdapter.ITEM_TYPE_RED_STYLE_2
            else -> ChatContentListAdapter.ITEM_TYPE_RED_STYLE_0
        }
    }

    fun computeResult(isRefresh: Boolean) {
        if (isRefresh) {
            initUserSize = 5
        } else {
            initUserSize = 28
            userResultList.clear()
        }

        val refreshResultList = getUserSpeakData(initUserSize)

        if (refreshResultList.isNotEmpty()) {
            userResultList.addAll(0, refreshResultList)
            // 将内容发射到界面
            userResultLiveData.postValue(userResultList)
        }
    }

    fun replyComments(size: Int) {
        val refreshResultList = getUserSpeakData(size)
        if (refreshResultList.isNotEmpty()) {
            // 将内容发射到界面
            replyCommentsLiveData.postValue(refreshResultList)
        }
    }

    fun initUserGroup(contentType:Int) {
        initMsg(contentType)
        val content = SPFAppConfig.userGroup
        userGroupLiveData.postValue(
            gson.fromJson(
                content,
                object : TypeToken<MutableList<MessageChatBean>>() {}.type
            )
        )
    }

    private fun initMsg(contentType: Int) {
        val content = if (contentType == FragChatContent.RED_GROUP_DRAMA) {
            SPFAppConfig.msgDrama
        } else {
            SPFAppConfig.msg
        }
        userSpeakLiveData.postValue(
            gson.fromJson(
                content,
                object : TypeToken<MutableList<BeanUserSpeak>>() {}.type
            )
        )
    }

}